package com.hczt.xhminiapp.adminapi.conf.security;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request;

/**
 * @author 红创-马海强
 * @date 2019-04-01 13:56
 * @说明：
 */
public class SecurityUtils {
    /**
     * 获取当前用户名
     * @return
     */
    public static String getCurrentUsername(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return (String) authentication.getPrincipal();
    }

    /**
     * 获取当前客户端信息
     * @return
     */
    public static OAuth2Request getCurrentOAuth2Request(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return ((OAuth2Authentication) authentication).getOAuth2Request();
    }

    /**
     * 获取客户端ID
     * @return
     */
    public static String getCurrentClientId(){
        return getCurrentOAuth2Request().getClientId();
    }

}
